package com.hundsun.aitest.controller;

import cn.hutool.db.Entity;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hundsun.aitest.common.Result;
import com.hundsun.aitest.model.ai.ParserScene;
import com.hundsun.aitest.model.entity.PlatConfig;
import com.hundsun.aitest.model.entity.PlatConfigExample;
import com.hundsun.aitest.model.plat.InterfaceDict;
import com.hundsun.aitest.service.DataBaseService;
import com.hundsun.aitest.service.PlatConfigService;
import com.hundsun.aitest.service.TestCasesService;
import com.hundsun.aitest.service.TestInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class PlatOperationController {

    @Autowired
    private TestInterfaceService testInterfaceService;

    @Autowired
    private TestCasesService testCasesService;

    @Autowired
    private DataBaseService dbConnectService;

    @Autowired
    private PlatConfigService platConfigService;

    @GetMapping("/getProjects")
    @ResponseBody
    public Result<String> handleGetProjects() {
        Result<String> result = new Result<>();
        PlatConfigExample example = new PlatConfigExample();
        PlatConfigExample.Criteria criteria = example.createCriteria();
        criteria.andTargetEqualTo("projects");
        List<PlatConfig> projects = platConfigService.selectByExample(example);
        result.setSuccess(true);
        result.setData(JSON.toJSONString(projects));
        return result;
    }

    @GetMapping("/actSql")
    @ResponseBody
    public Result<String> handleActSQL(@Param("project") String project, @Param("sql") String sql) {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        dbConnectService.initProject(project);
        List<Entity> patternResult = dbConnectService.actSql(sql);
        JSONObject jsonObject1 = JSONUtil.parseObj(patternResult);
        log.info("handlePatternInterfaceDetail:{}", jsonObject1);
        result.setData(jsonObject1.toString());
        return result;
    }

    @GetMapping("/queryTableStruct")
    @ResponseBody
    public Result<String> handleQueryTableStruct(@Param("project") String project, @Param("table") String table) {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        dbConnectService.initProject(project);
        dbConnectService.readTableStructure(table);
        return result;
    }

    @GetMapping("/patternInterfaceDetail")
    @ResponseBody
    public Result<String> handlePatternInterfaceDetail(@Param("testPoint") String testPoint,
                                                       @Param("project") String project) throws JsonProcessingException {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        InterfaceDict patternResult = testCasesService.patternAndGetInterfaceDetail(project, testPoint);
        JSONObject jsonObject1 = JSONUtil.parseObj(patternResult);
        log.info("handlePatternInterfaceDetail:{}", jsonObject1);
        result.setData(jsonObject1.toString());
        return result;
    }

    @GetMapping("/patternInterface")
    @ResponseBody
    public Result<String> handlePatternInterface(@Param("testPoint") String testPoint,
                                                 @Param("isGetDetail") boolean isGetDetail) throws JsonProcessingException {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        InterfaceDict patternResult = testInterfaceService.patternInterface(testPoint, isGetDetail);
        patternResult.setTestPoint(testPoint);
        JSONObject jsonObject = JSONUtil.parseObj(patternResult);
        log.info("patternResult:{}", jsonObject);
        result.setData(jsonObject.toString());
        return result;
    }

    @GetMapping("/queryInterfaceDetail")
    @ResponseBody
    public Result<String> handleQueryInterfaceDetail(@Param("interfaceId") Integer interfaceId){
        Result<String> result = new Result<>();
        result.setSuccess(true);
        InterfaceDict detail = testInterfaceService.queryInterfaceDetail(interfaceId);
        result.setData(detail.toString());
        return result;
    }

    @GetMapping("/queryScriptContent")
    @ResponseBody
    public Result<String> handleQueryScriptContent(@Param("interfaceId") Integer interfaceId,
                                                   @Param("project") String project){
        Result<String> result = new Result<>();
        result.setSuccess(true);
        InterfaceDict interfaceDict = new InterfaceDict();
        interfaceDict.setInterfaceId(interfaceId);
        testCasesService.updateProjectInfo(project.toUpperCase());
        InterfaceDict scriptContent = testCasesService.queryScriptId(interfaceDict);
        JSONObject jsonObject2 = JSONUtil.parseObj(scriptContent);
        log.info("scriptContent:{}", jsonObject2);
        result.setData(jsonObject2.toString());
        return result;
    }

    @PostMapping("/queryInterfaceDetail")
    @ResponseBody
    public Result<String> handleStandardParserContent(@RequestBody Map<String, String> requestBody) throws JsonProcessingException {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        String parseContent = requestBody.get("parseContent");
        List<ParserScene> scenes = testInterfaceService.standardTestParseContent(parseContent);
        JSONArray jsonArray = JSONUtil.parseArray(scenes);
        log.info("scenes:{}", jsonArray);
        result.setData(JSON.toJSONString(jsonArray));
        return result;
    }
}
